// @charset "utf-8";
/**
 * 赚钱效应-zqxy-view
 */
define(function (require, exports, module) {
(async()=>{ 
	var zqxyModel = require('./zqxymodel');
	var zqxyChart = require('./zqxychart');
	var zqxyView = {
		param : {
			period : 'min',
			begin : 0,
			end : 0
		},
		init : async function () {
			var self = this;
			var tradingDay = await commonMethod.getTradingDay();
			if (tradingDay == 0) {
				var getTradeDayTimer = setTimeout(function () {
					self.init();
				}, 200);
				return;
			} else {
				clearTimeout(getTradeDayTimer);
			}
			self.param.period = 'min';
			self.param.begin = tradingDay;
			self.requestData();
			self.renderRisingProb(tradingDay);
			interactions.datePicker(tradingDay, 'zqxy', function (date) {
				self.param.period = 'min';
				self.param.begin = date;
				self.requestData();
				self.renderRisingProb(date);
			}, function (startDate, endDate) {
				self.param.period = 'day';
				self.param.begin = startDate;
				self.param.end = endDate;
				self.requestData();
			});
			self.hoverChart();
		},
		isSuccessReg : false,
		autoRefresh : async function (isIn) {
			var self = this;
			var curtime = await commonMethod.serverTime();
			var isTradeTime = commonMethod.thsQuote.isTradeTime('1A0001');
			if ((self.param.period == 'min' && curtime == self.param.begin && isTradeTime) || isIn) {
				if (!self.isSuccessReg) {
					self.requestData();
				}
				self.renderRisingProb(self.param.begin);
			}
		},
		zqxyData : [],
		xAxisDate : [],
		shData : [],
		newZqxyData : '--',
		newShData : '--',
		reqTimer : null,
		requestTimer : function () {
			var self = this;
			if (isReqZqxy && isReqSh) {
				clearTimeout(self.reqTimer);
			} else {
				self.requestData();
				// self.reqTimer = setTimeout(function () {
				// 	self.requestTimer();
				// }, 1000);
			}
		},
		requestData : async function () {
			var self = this;
			self.zqxyData = [];
			self.shData = [];
			var curtime = await commonMethod.serverTime();
			var bool0930 = await commonMethod.judgeIntime(['00:00', '09:30']);
			if (self.param.period == 'min' && curtime == self.param.begin && bool0930) {
				self.renderReadText('--');
				zqxyChart.render(self.zqxyData, self.shData, self.param, self.xAxisDate);
			}
			await zqxyModel.request(self.param, function (result) {
				// alert(JSON.stringify(result));
				if (self.param.period == 'min') {
					self.zqxyData = result;
				} else {
					self.xAxisDate = result.date;
					result = result.arr;
					self.zqxyData = result;
				}
				self.newZqxyData = result[result.length - 1];
				if (self.shData.length) {
					self.analyticalData();
					self.register();
				}
			});
			zqxyModel.requestSh(self.param, function (result) {
				// alert(JSON.stringify(result));
				if (self.param.period == 'min') {
					self.shData = result;
				} else {
					result = result.arr;
					self.shData = result;
				}
				self.newShData = result[result.length - 1];
				if (self.zqxyData.length) {
					self.analyticalData();
					self.register();
				}
			});
			self.reqTimer = setTimeout(function () {
				self.requestTimer();
			}, 1000);
		},
		register : async function () {
			var self = this;
			var curtime = await commonMethod.serverTime();
			var isTradeTime = commonMethod.thsQuote.isTradeTime('1A0001');
			if (self.param.period == 'min' && curtime == self.param.begin && isTradeTime) {
				await clientQuoteBase.registerPush(self.param.begin, self.registerCall, self);
				self.isSuccessReg = true;
			} else {
				commonMethod.thsQuote.unregisterPush();
				self.isSuccessReg = false;
			}
		},
		registerCall : function (zqxyArr, shArr) {
			var self = this;
			if (!zqxyArr.length || !shArr.length) {
				self.isSuccessReg = false;
				self.requestData();
			}
			self.zqxyData = zqxyArr;
			self.shData = shArr;
			if (zqxyArr.length && zqxyArr[zqxyArr.length - 1]) {
				self.newZqxyData = zqxyArr[zqxyArr.length - 1];
			} else {
				self.newZqxyData = '--';
			}
			self.analyticalData();
		},
		analyticalData : function () {
			var self = this;
			if (self.zqxyData && self.shData) {
				self.renderReadText(self.newZqxyData);
				zqxyChart.render(self.zqxyData, self.shData, self.param, self.xAxisDate);
			} else {
				self.renderReadText('--');
			}
			// if (!$.isEmptyObject(result)) {
			// 	var ztData = [];
			// 	var dtData = [];
			// 	var fyzztData = [];
			// 	if (self.param.period === 'min') {
			// 		$.each(result.data, function (i, val) {
			// 			ztData.push(val.zt);
			// 			dtData.push(val.dt);
			// 			fyzztData.push(val.fyzzt);
			// 		});
			// 		self.newZtData = ztData[ztData.length - 1];
			// 		self.newDtData = dtData[dtData.length - 1];
			// 		self.newFyzztData = fyzztData[fyzztData.length - 1];

			// 		self.renderReadText(self.newZtData);
			// 	} else {
			// 		$.each(result, function(j, value) {
			// 			ztData.push(value.zt);
			// 			dtData.push(value.dt);
			// 			fyzztData.push(value.fyzzt);
			// 		});
			// 		self.newZtData = ztData[ztData.length - 1];
			// 		self.newDtData = dtData[dtData.length - 1];
			// 		self.newFyzztData = fyzztData[fyzztData.length - 1];
			// 	}
			// 	$('#view_a_readingbtn').removeClass('cannot-read');
			// } else {
			// 	self.newZtData = '--';
			// 	self.newDtData = '--';
			// 	self.newFyzztData = '--';
			// 	$('#view_a_readingbtn').addClass('cannot-read');
			// 	self.renderReadText('--');
			// }
			// qrpjChart.render(result, self.param);
		},
		renderReadText : async function (newZqxyData) {
			var ratingText = '--';
			var curtime = await commonMethod.serverTime();
			if (newZqxyData !== '--' && newZqxyData != null) {
				if(newZqxyData > 4 ) {
					ratingText = '好';
				}else if(newZqxyData > 2.5 && newZqxyData <= 4){
					ratingText = '较好';
				}else if(newZqxyData >1.5 && newZqxyData <= 2.5){
					ratingText = '中性';
				}else if(newZqxyData > 0.5 && newZqxyData <= 1.5){
					ratingText = '较差';
				}else if(newZqxyData <= 0.5) {
					ratingText = '差';
				}
				var ratingImg = 1;
				if(newZqxyData <= -0.5){
					ratingImg = 1;
				}else if(newZqxyData <= 0.5){
					ratingImg = 2;
				}else if(newZqxyData <= 1){
					ratingImg = 3;
				}else if(newZqxyData <= 1.5){
					ratingImg = 4;
				}else if(newZqxyData <= 2){
					ratingImg = 5;
				}else if(newZqxyData <= 2.5){
					ratingImg = 6;
				}else if(newZqxyData <= 3){
					ratingImg = 7;
				}else if(newZqxyData <= 4){
					ratingImg = 8;
				}else if(newZqxyData <= 6){
					ratingImg = 9;
				}else if(newZqxyData <= 10){
					ratingImg = 10;
				}
				$('#reading-zqxyimg').attr('src', 'images/unscramble2-'+ ratingImg +'.jpg');

				// if (this.param.begin == curtime && await commonMethod.judgeIntime(['08:50', '09:45'])) {
				// 	$('#view_a_readingbtn').addClass('cannot-read');
				// } else {
				$('#view_a_readingbtn').removeClass('cannot-read');
				// }
			} else {
				$('#view_a_readingbtn').addClass('cannot-read');
			}
			$('#view_strong_zqxyread').text(ratingText);
		},
		hoverChart : function () {
			var self = this;
			$('#view_div_zqxychart').unbind('mouseleave').mouseleave(function() {
				$('.zqxy-legend:eq(0)').text(self.newZqxyData);
				$('.zqxy-legend:eq(1)').text(self.newShData);
			});
		},
		renderRisingProb : async function (startDate) {
			var serverTime = await commonMethod.serverTime();
			var type = 'zqxypjhc';
			if (serverTime == startDate && await commonMethod.judgeIntime(['00:01', '09:45'])) {
				interactions.renderRisingProb(type, 'NUL');
			} else {
				interactions.requestZxData(type, startDate);
			}
		}
	};
	module.exports = zqxyView;
})();
});